/**
 * @Title: ProductSaleController.java
 * @Description: TODO(用一句话描述该文件做什么)
 * @author 张鑫
 * @phone 17561783855
 * @date 2019年5月22日
 */
package com.fortune.rainshine.service.controller;

import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fortune.rainshine.common.BaseController;
import com.fortune.rainshine.service.service.ProductSaleService;
import com.fortune.rainshine.service.service.StockService;
import com.fortune.rainshinecommon.base.MsgModel;
import com.fortune.rainshinecommon.enums.ResultCode;
import com.fortune.rainshinecommon.model.service.StockRecordsModel;

/**
 * @ClassName: ProductSaleController
 * @Description: TODO(店铺产品处理 - - 产品销售以及产品领用)
 * @author 张鑫
 * @phone 17561783855
 * @date 2019年5月22日
 *
 */
@Controller
@RequestMapping("/service/productUse")
@MultipartConfig
public class ProductUseController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(ProductUseController.class);
    @Autowired
    private ProductSaleService service;
    @Autowired
    private StockService stockService;


    @RequiresPermissions("productUse:view")
    @RequestMapping()
    public String product(Model model, HttpServletRequest request) {
        //获取当前商铺id
    	String parm = request.getHeader("Pass-Check-Session");
    	String parm2 = request.getHeader("Pass-Check-Session").substring(16, parm.length());
    	String shopId = parm2.substring(0,parm2.length()-9);
        //根据店铺id获取该店库存详情
        Object stocks = stockService.getStocksForShopUse(shopId).getData();
        model.addAttribute("stocks", stocks);
        return "productUse/productSaleList.html";
    }



    @RequiresPermissions("productUse:use")
    @ResponseBody
    @RequestMapping("/use")
    public MsgModel use(@RequestBody StockRecordsModel model) {
        try {
            //model.setUser(this.getLoginUser());
            model.setUserId(this.getLoginUserId());
            model.setCreateId(this.getLoginUser().getUserId());
            return service.add(model);
        } catch (Exception e) {
            logger.error("use ======> {}", e);
            return new MsgModel(ResultCode.FAILURE);
        }
    }
}
